package ca.jsbr.canvaswar.games 
{
	import ca.jsbr.as2js_lib.display.canvas.Node2D;
	import ca.jsbr.as2js_lib.event.BasicEvent;
	import ca.jsbr.canvaswar.games.mission.Mission;
	import js.window;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class MissionManager 
	{
		private var _layer:Node2D;
		private var _mission:Mission;
		static private var _instance:MissionManager;
		
		public function MissionManager() 
		{
			_layer = new Node2D();;
		}
		
		public function launchMission(name:String):void
		{
			if (_mission)
			{
				_mission.destroy();
				_layer.removeChild(_mission);
			}
				
			_mission = new window[name]();
			_mission.init();
			if (_mission.ready)
				startMission();
			else
				addLoading();
			//_layer.addChild()
		}
		
		public function addLoading():void
		{
			_mission.addEventListener(BasicEvent.READY, onReady);
		}
		
		private function onReady(e:BasicEvent):void 
		{
			startMission();
		}
		
		public function startMission():void
		{
			_layer.addChild(_mission);
			_mission.start();
		}
		
		public function destroy():void
		{
			instance = null;
		}
		
		public function destroy():void
		{
			_instance = null;
		}
				
		public static function getInstance():MissionManager
		{
			if (!_instance)
				_instance = new MissionManager();
			return _instance;
		}
		
		public function get layer():Node2D 
		{
			return _layer;
		}
		
	}

}